package ToYesterday.Day19_isValidBST;

import org.junit.Test;

import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    TreeNode node;
    private List<Integer> lists;

    public void init() {
        node = new TreeNode(4);
        node.left = new TreeNode(-1);
        node.right = new TreeNode(8);
        node.left.left = new TreeNode(11);
        node.left.right = new TreeNode(3);
    }

    long preNode = Long.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
//        if (root==null)return true;
//        if (((root.left!=null)&&root.val<=root.left.val)||(root.right!=null)&&(root.val>=root.right.val)){
//            return false;
//        }
//
//        boolean bleft= isValidBST(root.left);
//        boolean bright= isValidBST(root.right);
//        return bright&&bleft;


//        lists = new ArrayList<>();
//        middleSort(root);
//        for (int i =0;i<lists.size()-1;i++){
//            if (lists.get(i)<lists.get(i+1))continue;
//            else return false;
//        }

        if (root == null) return true;
        boolean leftb=true;
        boolean rightb=true;
        if (root.left != null)
            leftb = isValidBST(root.left);
        if (root.val > preNode) {
            preNode =(long)root.val;
        } else return false;

        if (root.right != null)
            rightb=isValidBST(root.right);
        return rightb && leftb;

    }
//    public void middleSort(TreeNode node){
//        if (node == null)return;
//        if (node.left!=null)middleSort(node.left);
//        lists.add(node.val);
//        if (node.right!=null)middleSort(node.right);
//    }

    @Test
    public void TestNode() {
        init();
        boolean isvalid = isValidBST(node);
        System.out.println(isvalid);
    }
}
